import tos
import os
import sys
sys.path.append('/data/code/')
from data_get import get_data_task
import concurrent.futures
import logging
import time

# Access key 和 Secret key 可在用户火山引擎账号中查找
ak = "AKLTMmFmMzVmNmY5ZDEyNDNmNWEyZTU2MDM3Y2EzMDJlZTk"
sk = "WVdNeFpXRXdNekV4Tm1ZME5EZGxPRGc0TUdFNE1HWXdNV1ZqTkdNNU9HRQ=="
# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例，your endpoint 填写 tos-cn-beijing.volces.com，your region 填写 cn-beijing。
endpoint = "tos-cn-beijing.ivolces.com"
region = "cn-beijing"

def get_tos_path(bucket_name,prefix):
    pathlist=[]
    try:
        # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
        client = tos.TosClientV2(ak, sk, endpoint, region)

        # 列举指定桶下特定前缀所有对象
        truncated = True
        continuation_token = ''

        while truncated:
            result = client.list_objects_type2(bucket_name, prefix=prefix, continuation_token=continuation_token)
            for iterm in result.contents:
                path = str(iterm.key)

                arr = path.split("/")
                filename=arr[6]
                filearr=filename.split("-")
                file_code=filearr[4]
                # print("file_code: "+file_code)
                if file_code.replace(".zip","") in ["2300","2301","2302","2303","2304","2305","2309","2310","2311","2314","3307","3308"]:
                    print("path: "+path)
                    file_list = filename.split("-")
                    file=file_list[0]
                    pathlist.append(path)
            truncated = result.is_truncated
            continuation_token = result.next_continuation_token
    except tos.exceptions.TosClientError as e:
        # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
        print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
    except tos.exceptions.TosServerError as e:
        # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
        print('fail with server error, code: {}'.format(e.code))
        # request id 可定位具体问题，强烈建议日志中保存
        print('error with request id: {}'.format(e.request_id))
        print('error with message: {}'.format(e.message))
        print('error with http code: {}'.format(e.status_code))
    except Exception as e:
        print('fail with unknown error: {}'.format(e))

    return  pathlist

def getvin(bucket_name,fold):
    pathlist = []
    try:
        # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
        client = tos.TosClientV2(ak, sk, endpoint, region)
        # 列举指定桶下特定前缀所有对象)
        truncated = True
        continuation_token = ''
        while truncated:
            result = client.list_objects_type2(bucket_name, prefix=fold, delimiter="/", continuation_token=continuation_token, max_keys=1000)

            for prefix in result.common_prefixes:
                prefix_dir = prefix.prefix
                # print("prefix_dir: "+prefix_dir)
                dirs = prefix_dir.split('/')
                pathlist.append(dirs[3])
            truncated = result.is_truncated
            continuation_token = result.next_continuation_token

    except Exception as e:
        print('fail with unknown error: {}'.format(e))
    return  list(set(pathlist))

def task(bucket_name,arr_source):
    prefixlist=[]
    for vehicle_id in arr_source:
        prefix1 = "EP36/TDA4/SC/" + vehicle_id + "/" + "20240204" + "/" + "8" + "/"
        print(prefix1)
        pathlist = get_tos_path(bucket_name, prefix1)
        for object_key in pathlist:
            print("object_key: " + object_key)
            prefixlist.append(object_key)
    return list(set(prefixlist))
def task_excep(object_key,bucket_name):
    try:
        get_data_task(object_key,bucket_name)
    except Exception as e:
        print('label mark error, ', str(e))
if __name__ == '__main__':
    os.chdir('/data/code/')
    bucket_name="advc-data-upload-2100095994-cn-beijing"
    prefix_source="EP36/TDA4/SC/"
    arr_source = getvin(bucket_name, prefix_source)
    print("arr_source：" + str(arr_source))
    prefixlist=task(bucket_name, arr_source)
    print("prefixlist："+str(prefixlist))
    for object_key in prefixlist:
        logging.info("object_key: "+object_key)
        start = time.time()
        task_excep(object_key, bucket_name)
        end = time.time()
        if(end-start)>180:
            sys.exit()
    logging.info("任务执行完成。。。")
    sys.exit()
    # with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
    #     # 提交任务到线程池
    #     futures = [executor.submit(task_excep, object_key, bucket_name) for object_key in prefixlist]
